package admin.code.generator;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import admin.code.generator.common.ConstantTemplateKey;
import admin.code.generator.core.CodeGenerator;
import admin.code.generator.core.metadata.BaseMetaData;
import admin.code.generator.core.metadata.TableMetaData;
import admin.code.generator.core.metadata.ViewMetaData;
import admin.code.generator.util.ClassPathMaker;
import admin.code.generator.util.ConfigUtils;
import admin.code.generator.util.UpperFirstCharacter;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateNotFoundException;

/**
 * Hello world!
 *
 */
public class App {

	public static void main(String[] args) throws Exception {
		if (args == null || args.length < 2) {
			System.err.println("input error!");
			System.out.println("usage: java App configFile tableName1 tableName2 ...");
			System.exit(0);
		}
		String cfgFile = args[0];

		ConfigUtils.loadCfgFile(cfgFile);

		String destDir = ConfigUtils.get("diskPath");
		String projectName = ConfigUtils.get("project");
		String packageName = ConfigUtils.get("basePackage");

		for (int i = 1; i < args.length; i++) {
			String tableName = args[i];
			doGenerate(tableName, destDir, projectName, packageName);
		}

		//String tableName = "t_game";
	}

	private static void doGenerate(String tableName, String destDir, String projectName, String packageName) throws Exception, TemplateNotFoundException, MalformedTemplateNameException,
			ParseException, IOException {

		TableMetaData tmd = new TableMetaData(tableName);

		BaseMetaData bmd = new BaseMetaData(packageName, tmd);

		Map root = bmd.fillTempateModelData();

		ClassPathMaker pathMaker = new ClassPathMaker(destDir, projectName, packageName);

		//生成java类,domain,Controller,Service,ServiceImpl,Dao,DaoImpl
		CodeGenerator.generate(root, getConfig().getTemplate("bean.ftl"), pathMaker.buildDomainPath(), bmd.getDomainName() + ".java");
		CodeGenerator.generate(root, getConfig().getTemplate("controller.ftl"), pathMaker.buildControllerPath(), bmd.getControllerName() + ".java");
		CodeGenerator.generate(root, getConfig().getTemplate("service.ftl"), pathMaker.buildServicePath(), bmd.getServiceName() + ".java");
		CodeGenerator.generate(root, getConfig().getTemplate("serviceImpl.ftl"), pathMaker.buildServiceImplPath(), bmd.getServiceImplName() + ".java");
		CodeGenerator.generate(root, getConfig().getTemplate("dao.ftl"), pathMaker.buildDaoPath(), bmd.getDaoName() + ".java");
		CodeGenerator.generate(root, getConfig().getTemplate("daoImpl.ftl"), pathMaker.buildDaoImplPath(), bmd.getDaoImplName() + ".java");

		//生成Html 文档
		ViewMetaData vmd = new ViewMetaData(tmd);
		Map htmlRoot = vmd.fillTempateModelData();
		String htmlsDir = ConfigUtils.get("htmlsDir") + File.separator + vmd.getControllerMapping();
		htmlRoot.put(ConstantTemplateKey.HTML_TITLE, "新增");
		CodeGenerator.generate(htmlRoot, getConfig().getTemplate("form.html"), htmlsDir, "form.html");
		htmlRoot.put(ConstantTemplateKey.HTML_TITLE, "列表");
		CodeGenerator.generate(htmlRoot, getConfig().getTemplate("list.html"), htmlsDir, "list.html");
		htmlRoot.put(ConstantTemplateKey.HTML_TITLE, "详情");
		CodeGenerator.generate(htmlRoot, getConfig().getTemplate("view.html"), htmlsDir, "view.html");
	}

	private static Configuration getConfig() {
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
		cfg.setClassForTemplateLoading(CodeGenerator.class, "/templates");
		cfg.setDefaultEncoding("UTF-8");
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		cfg.setSharedVariable("upperFC", new UpperFirstCharacter()); //添加一个"宏"共享变量用来将属性名首字母大写   
		return cfg;
	}

}
